package com.iamteer.day20;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;


/**
主要回顾：IO 异常处理
 */
public class ProcessIOException {
	public static void main(String[] args) {
		testProcess();
	}
	
	public static void testProcess() {
		//1. 找到目标文件
		File file = new File("test1.txt");
		FileInputStream fileInputStream = null;
		try {
			//2. 建立数据通道
			fileInputStream = new FileInputStream(file);
			//3. 读取文件数据
			byte[] buf = new byte[1024];	//建立缓冲数组
			int length = -1;				//记录读取长度
			while ((length = fileInputStream.read(buf)) != -1 ) {
				System.out.print(new String(buf, 0, length));
			}
		} catch (IOException e) {
			System.out.println("读取文件资源失败。。。");
			//处理异常
			throw new RuntimeException(e);
			//把 IOException 传递给 RuntimeException 包装一层再抛出，目的是方便调用者
		}finally {
			try {
				if (fileInputStream != null) {
					fileInputStream.close();
					System.out.println("\n关闭资源成功！");
				}
			} catch (IOException e) {
				System.out.println("\n关闭资源失败！");
				e.printStackTrace();
			}
		}
	}
}
